import Vue from 'vue'
import VueRouter from 'vue-router'
import Demo from '../components/Demo.vue'
import Home from '../views/Home.vue'
import Less from '../components/less/Less.vue';
import Variable from '../components/less/Variable.vue';
import Login from '../views/Login.vue';
import Welcome from '../views/Welcome.vue';
import Mixins from '../components/less/Mixins.vue';
import NestedRules from '../components/less/nestedRules.vue';
import operations from '../components/less/Operations.vue';
import Namespace from '../components/less/Namespace.vue';
import Important from '../components/less/Import.vue';

Vue.use(VueRouter)

const routes = [
    { path: '/',redirect:'/login'},
    { path: '/login',component: Login},
    { 
      path: '/home',
      component: Home,
      redirect:'/welcome',
      children:[
      { path : '/welcome' , component: Welcome},
      { path : '/less' , component: Less},
      { path : '/variable' , component: Variable},
      { path : '/mixins' , component: Mixins},
      { path : '/nestedRules' , component: NestedRules},
      { path : '/operations' , component: operations},
      { path : '/namespace' , component: Namespace},
      { path : '/important' , component: Important}
    ]},
    { path: '/demo',component: Demo}
]

const router = new VueRouter({
  routes
})

//挂载路由导航前卫
router.beforeEach((to,from,next) => {
  if(to.path === '/login') return next()
  //获取token
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()

})

export default router
